﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace Alex.Library.Log.Configuration
{
    /// <summary>
    /// 文件日志节点
    /// </summary>
    public class FileLoggerElement : LoggerElement
    {
        /// <summary>
        /// 日志保存目录
        /// </summary>
        [ConfigurationProperty("logDir", IsRequired = true)]
        public LogDirectoryContentElement LogDir
        {
            get
            {
                return (LogDirectoryContentElement)this["logDir"];
            }
        }

        /// <summary>
        /// 日志文件类型，默认是.log
        /// </summary>
        [ConfigurationProperty("fileExtension", IsRequired = false)]
        public FileExtensionContentElement FileExtension
        {
            get
            {
                return (FileExtensionContentElement)this["fileExtension"];
            }
        }

        /// <summary>
        /// 如果存在同名文件是否覆盖
        /// </summary>
        [ConfigurationProperty("overrideIfExist", IsRequired = false)]
        public BooleanContentElement OverrideIfExist
        {
            get
            {
                return (BooleanContentElement)this["overrideIfExist"];
            }
        }

        /// <summary>
        /// 创建日志文件的行为
        /// </summary>
        [ConfigurationProperty("behavior", IsRequired = false)]
        public CreateFileBehaviorContentElement Behavior
        {
            get
            {
                return (CreateFileBehaviorContentElement)this["behavior"];
            }
        }

        /// <summary>
        /// 文件大小的上限
        /// </summary>
        [ConfigurationProperty("maxAchieveSize", IsRequired = false)]
        public DoubleContentElement MaxAchieveSize
        {
            get
            {
                return (DoubleContentElement)this["maxAchieveSize"];
            }
        }

        /// <summary>
        /// 根据配置生成文件日志对象
        /// </summary>
        /// <returns></returns>
        public override ILogger GetLogger()
        {
            FileLogger fl = new FileLogger(this.LogDir.Value);
            fl.FileExtension = this.FileExtension.Value;
            fl.OverrideIfExist = this.OverrideIfExist.Value;
            fl.Behavior = this.Behavior.Value;
            fl.MaxAchieveSize = this.MaxAchieveSize.Value;
            fl.FlushLevel = this.FlushLevel.Value;
            fl.Template = this.Template.GetTemplate();
            return fl;
        }
    }
}
